﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EduProject.Database;
using EduProject.Areas.User.Models;
using System.Data;
using System.Web.Security;
using EduProject.Models;
using System.Text;

namespace EduProject.Areas.User.Controllers
{
    public class AccountController : Controller
    {
        ProductInfo NewProduct = new ProductInfo();
        loginData logData = new loginData();
        BShopEntities shopEntity = new BShopEntities();
        //
        // GET: /User/Account/
        public ActionResult Index()
        {
            return View();
        }
        //用户中心
        public ActionResult UserCenter(string id)
        {
            if (Session["UserName"] == null)
            {
                //string script = string.Format("<script>alert('登陆超时或你尚未进行登陆！');location.href='/User/Account/Login'</script>");
                //return Content(script, "text/html");
                ViewBag.name = "";
            }
            else
            {
                string name = Session["UserName"].ToString();
                ViewBag.name = name;
                //获取用户的信息
                var userInfo = shopEntity.User.Where(c => c.UName == name).SingleOrDefault();
                ViewBag.Name = userInfo.UName;
                ViewBag.userImage = userInfo.Image;
                ViewBag.UserId = userInfo.Id;
                //状态为01表示等待付款 状态为02表示已过期 状态为03表示已付款 状态为04表示交易完成
                if (id == "00" || id == null)
                {
                    //查询所有订单，获取订单号，订单总金额
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name).ToList();
                    ViewBag.orderInfo = OrderInfo;
                }
                else if (id == "01")
                {
                    //查询所有订单状态为01的订单
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name && c.Status == "01").ToList();
                    ViewBag.orderInfo = OrderInfo;
                }
                else if (id == "02")
                {
                    //查询所有订单状态为02的订单
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name && c.Status == "02").ToList();
                    ViewBag.orderInfo = OrderInfo;
                }
                else if (id == "03")
                {
                    //查询所有订单状态为03的订单
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name && c.Status == "03").ToList();
                    ViewBag.orderInfo = OrderInfo;
                }
                else
                {
                    //查询所有订单状态为04的订单
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name && c.Status == "04").ToList();
                    ViewBag.orderInfo = OrderInfo;
                }

            }
            return View();
        }



        [HttpPost]
        public string UserCenterInfo(string id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"<tr class='order_list_title'>
                <th style='width: 191px'>订单信息</th>
                <th style='width: 80px'>订购商品</th>
                <th style='width: 40px'>件数</th>
                <th style='width: 80px'>单价</th>
                <th style='width: 70px'>商品操作</th>
                <th style='width: 120px'>订单状态</th>
                <th style='width: 90px'>订单操作</th>
            </tr>");
            if (Session["UserName"] == null)
            {
                return sb.ToString();
            }
            else
            {
                string name = Session["UserName"].ToString();
                //状态为01表示等待付款 状态为02表示已过期 状态为03表示已付款 状态为04表示交易完成
                if (id == "00" || id == null)
                {
                    //查询所有订单，获取订单号，订单总金额
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name).ToList();
                    foreach (var item in OrderInfo)
                    {
                        for (int i = 0; i < item.OrderDetail.Count; i++)
                        {
                            if (i == 0)
                            {
                                sb.Append(@"<tr>
                                <td class='order_info_td' rowspan=@item.OrderDetail.Count>
                                <p>交易单号：@item.Id</p>
                                <p>商品金额: @item.TotalPrice</p>
                                <p> 由 美妆商城 发货 </p>
                                </td>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[0].ProductId' target='_blank' title=@item.OrderDetail[0].PName>
                                    <img src=@item.OrderDetail[0].pic alt=@item.OrderDetail[0].PName />
                                </a>
                                </td>

                                <td>@item.OrderDetail[0].Count</td>

                                <td>¥@{@item.OrderDetail[0].Price}</td>

                                <td></td>");

                                // 状态为01表示等待付款 状态为02表示已过期 状态为03表示已付款 状态为04表示交易完成 
                                if (item.Status == "01")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_red'>等待付款</span>
                                    </td>
                                    <td rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=c27235512780'>查看详情</a></p>

                                    <p><a href='http://cart.jumei.com/cashier?confirmationId=c27235512780&address_id=162859155&orderIds=851162202' class='btn_mid_pink'>点击付款</a></p>

                                    <p>
                                        <a href='/i/order/cancel/?order_id=c27235512780' order-id='c27235512780' class='order-cancel'>取消</a>
                                    </p>
                                    <p><a href='/i/order/goto_modify_shipping_by_order/?order_id=c27235512780'>修改地址</a></p>
                                    </td>");
                                }
                                if (item.Status == "02")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_grey'>已过期</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "03")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>已付款</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "04")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>交易完成</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                sb.Append(@"</tr>");
                            }
                            else
                            {
                                sb.Append(@"
                                <tr>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[i].ProductId' target='_blank' title=@item.OrderDetail[i].PName>
                                    <img src=@item.OrderDetail[i].pic alt=@item.OrderDetail[i].PName onerror='onImageLoadError.apply(this);' />
                                </a>
                                </td>
                                <td>@item.OrderDetail[i].Count</td>
                                <td>¥@item.OrderDetail[i].Price</td>
                                <td></td>
                                </tr>");
                            }
                        }
                        sb.Append(@" <tr class='separator'>
                        <td colspan='7'></td>
                        </tr>");
                    }

                }
                else if (id == "01")
                {
                    //查询所有订单状态为01的订单
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name && c.Status == "01").ToList();
                    foreach (var item in OrderInfo)
                    {
                        for (int i = 0; i < item.OrderDetail.Count; i++)
                        {
                            if (i == 0)
                            {
                                sb.Append(@"<tr>
                                <td class='order_info_td' rowspan=@item.OrderDetail.Count>
                                <p>交易单号：@item.Id</p>
                                <p>商品金额: @item.TotalPrice</p>
                                <p> 由 美妆商城 发货 </p>
                                </td>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[0].ProductId' target='_blank' title=@item.OrderDetail[0].PName>
                                    <img src=@item.OrderDetail[0].pic alt=@item.OrderDetail[0].PName />
                                </a>
                                </td>

                                <td>@item.OrderDetail[0].Count</td>

                                <td>¥@{@item.OrderDetail[0].Price}</td>

                                <td></td>");

                                // 状态为01表示等待付款 状态为02表示已过期 状态为03表示已付款 状态为04表示交易完成 
                                if (item.Status == "01")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_red'>等待付款</span>
                                    </td>
                                    <td rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=c27235512780'>查看详情</a></p>

                                    <p><a href='http://cart.jumei.com/cashier?confirmationId=c27235512780&address_id=162859155&orderIds=851162202' class='btn_mid_pink'>点击付款</a></p>

                                    <p>
                                        <a href='/i/order/cancel/?order_id=c27235512780' order-id='c27235512780' class='order-cancel'>取消</a>
                                    </p>
                                    <p><a href='/i/order/goto_modify_shipping_by_order/?order_id=c27235512780'>修改地址</a></p>
                                    </td>");
                                }
                                if (item.Status == "02")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_grey'>已过期</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "03")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>已付款</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "04")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>交易完成</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                sb.Append(@"</tr>");
                            }
                            else
                            {
                                sb.Append(@"
                                <tr>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[i].ProductId' target='_blank' title=@item.OrderDetail[i].PName>
                                    <img src=@item.OrderDetail[i].pic alt=@item.OrderDetail[i].PName onerror='onImageLoadError.apply(this);' />
                                </a>
                                </td>
                                <td>@item.OrderDetail[i].Count</td>
                                <td>¥@item.OrderDetail[i].Price</td>
                                <td></td>
                                </tr>");
                            }
                        }
                        sb.Append(@" <tr class='separator'>
                        <td colspan='7'></td>
                        </tr>");
                    }
                }
                else if (id == "02")
                {
                    //查询所有订单状态为02的订单
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name && c.Status == "02").ToList();
                    foreach (var item in OrderInfo)
                    {
                        for (int i = 0; i < item.OrderDetail.Count; i++)
                        {
                            if (i == 0)
                            {
                                sb.Append(@"<tr>
                                <td class='order_info_td' rowspan=@item.OrderDetail.Count>
                                <p>交易单号：@item.Id</p>
                                <p>商品金额: @item.TotalPrice</p>
                                <p> 由 美妆商城 发货 </p>
                                </td>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[0].ProductId' target='_blank' title=@item.OrderDetail[0].PName>
                                    <img src=@item.OrderDetail[0].pic alt=@item.OrderDetail[0].PName />
                                </a>
                                </td>

                                <td>@item.OrderDetail[0].Count</td>

                                <td>¥@{@item.OrderDetail[0].Price}</td>

                                <td></td>");

                                // 状态为01表示等待付款 状态为02表示已过期 状态为03表示已付款 状态为04表示交易完成 
                                if (item.Status == "01")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_red'>等待付款</span>
                                    </td>
                                    <td rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=c27235512780'>查看详情</a></p>

                                    <p><a href='http://cart.jumei.com/cashier?confirmationId=c27235512780&address_id=162859155&orderIds=851162202' class='btn_mid_pink'>点击付款</a></p>

                                    <p>
                                        <a href='/i/order/cancel/?order_id=c27235512780' order-id='c27235512780' class='order-cancel'>取消</a>
                                    </p>
                                    <p><a href='/i/order/goto_modify_shipping_by_order/?order_id=c27235512780'>修改地址</a></p>
                                    </td>");
                                }
                                if (item.Status == "02")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_grey'>已过期</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "03")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>已付款</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "04")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>交易完成</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                sb.Append(@"</tr>");
                            }
                            else
                            {
                                sb.Append(@"
                                <tr>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[i].ProductId' target='_blank' title=@item.OrderDetail[i].PName>
                                    <img src=@item.OrderDetail[i].pic alt=@item.OrderDetail[i].PName onerror='onImageLoadError.apply(this);' />
                                </a>
                                </td>
                                <td>@item.OrderDetail[i].Count</td>
                                <td>¥@item.OrderDetail[i].Price</td>
                                <td></td>
                                </tr>");
                            }
                        }
                        sb.Append(@" <tr class='separator'>
                        <td colspan='7'></td>
                        </tr>");
                    }
                }
                else if (id == "03")
                {
                    //查询所有订单状态为03的订单
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name && c.Status == "03").ToList();
                    foreach (var item in OrderInfo)
                    {
                        for (int i = 0; i < item.OrderDetail.Count; i++)
                        {
                            if (i == 0)
                            {
                                sb.Append(@"<tr>
                                <td class='order_info_td' rowspan=@item.OrderDetail.Count>
                                <p>交易单号：@item.Id</p>
                                <p>商品金额: @item.TotalPrice</p>
                                <p> 由 美妆商城 发货 </p>
                                </td>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[0].ProductId' target='_blank' title=@item.OrderDetail[0].PName>
                                    <img src=@item.OrderDetail[0].pic alt=@item.OrderDetail[0].PName />
                                </a>
                                </td>

                                <td>@item.OrderDetail[0].Count</td>

                                <td>¥@{@item.OrderDetail[0].Price}</td>

                                <td></td>");

                                // 状态为01表示等待付款 状态为02表示已过期 状态为03表示已付款 状态为04表示交易完成 
                                if (item.Status == "01")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_red'>等待付款</span>
                                    </td>
                                    <td rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=c27235512780'>查看详情</a></p>

                                    <p><a href='http://cart.jumei.com/cashier?confirmationId=c27235512780&address_id=162859155&orderIds=851162202' class='btn_mid_pink'>点击付款</a></p>

                                    <p>
                                        <a href='/i/order/cancel/?order_id=c27235512780' order-id='c27235512780' class='order-cancel'>取消</a>
                                    </p>
                                    <p><a href='/i/order/goto_modify_shipping_by_order/?order_id=c27235512780'>修改地址</a></p>
                                    </td>");
                                }
                                if (item.Status == "02")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_grey'>已过期</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "03")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>已付款</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "04")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>交易完成</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                sb.Append(@"</tr>");
                            }
                            else
                            {
                                sb.Append(@"
                                <tr>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[i].ProductId' target='_blank' title=@item.OrderDetail[i].PName>
                                    <img src=@item.OrderDetail[i].pic alt=@item.OrderDetail[i].PName onerror='onImageLoadError.apply(this);' />
                                </a>
                                </td>
                                <td>@item.OrderDetail[i].Count</td>
                                <td>¥@item.OrderDetail[i].Price</td>
                                <td></td>
                                </tr>");
                            }
                        }
                        sb.Append(@" <tr class='separator'>
                        <td colspan='7'></td>
                        </tr>");
                    }
                }
                else
                {
                    //查询所有订单状态为04的订单
                    List<Order> OrderInfo = shopEntity.Order.Include("OrderDetail").Where(c => c.CName == name && c.Status == "04").ToList();
                    foreach (var item in OrderInfo)
                    {
                        for (int i = 0; i < item.OrderDetail.Count; i++)
                        {
                            if (i == 0)
                            {
                                sb.Append(@"<tr>
                                <td class='order_info_td' rowspan=@item.OrderDetail.Count>
                                <p>交易单号：@item.Id</p>
                                <p>商品金额: @item.TotalPrice</p>
                                <p> 由 美妆商城 发货 </p>
                                </td>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[0].ProductId' target='_blank' title=@item.OrderDetail[0].PName>
                                    <img src=@item.OrderDetail[0].pic alt=@item.OrderDetail[0].PName />
                                </a>
                                </td>

                                <td>@item.OrderDetail[0].Count</td>

                                <td>¥@{@item.OrderDetail[0].Price}</td>

                                <td></td>");

                                // 状态为01表示等待付款 状态为02表示已过期 状态为03表示已付款 状态为04表示交易完成 
                                if (item.Status == "01")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_red'>等待付款</span>
                                    </td>
                                    <td rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=c27235512780'>查看详情</a></p>

                                    <p><a href='http://cart.jumei.com/cashier?confirmationId=c27235512780&address_id=162859155&orderIds=851162202' class='btn_mid_pink'>点击付款</a></p>

                                    <p>
                                        <a href='/i/order/cancel/?order_id=c27235512780' order-id='c27235512780' class='order-cancel'>取消</a>
                                    </p>
                                    <p><a href='/i/order/goto_modify_shipping_by_order/?order_id=c27235512780'>修改地址</a></p>
                                    </td>");
                                }
                                if (item.Status == "02")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_grey'>已过期</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "03")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>已付款</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                if (item.Status == "04")
                                {
                                    sb.Append(@"
                                    <td class='status' rowspan=@item.OrderDetail.Count>
                                    <span class='status_green'>交易完成</span>
                                    </td>
                                    <td class='OrderDetail' rowspan=@item.OrderDetail.Count>

                                    <p><a href='/i/order/detail/?order_id=850540663'>查看详情</a></p>
                                    </td>");
                                }
                                sb.Append(@"</tr>");
                            }
                            else
                            {
                                sb.Append(@"
                                <tr>
                                <td class='item_title'>
                                <a href='/User/Product/Single?id=@item.OrderDetail[i].ProductId' target='_blank' title=@item.OrderDetail[i].PName>
                                    <img src=@item.OrderDetail[i].pic alt=@item.OrderDetail[i].PName onerror='onImageLoadError.apply(this);' />
                                </a>
                                </td>
                                <td>@item.OrderDetail[i].Count</td>
                                <td>¥@item.OrderDetail[i].Price</td>
                                <td></td>
                                </tr>");
                            }
                        }
                        sb.Append(@" <tr class='separator'>
                        <td colspan='7'></td>
                        </tr>");
                    }
                }
            }
            return sb.ToString();
        }

        //登录
        public ActionResult login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult login(loginInfo info)
        {
            #region
            //if (ModelState.IsValid)
            //{
            //    string username = HttpUtility.HtmlEncode(info.username);
            //    string pwd = HttpUtility.HtmlEncode(info.password);
            //    loginData logData = new loginData();
            //    var userData = logData.getLoginData(username, pwd);
            //    if (userData.Count() <= 0)
            //    {
            //        string script = string.Format("<script>alert('用户名和密码不一致！');location.href='{0}'</script>", Url.Action("Login", "Account", "User"));
            //        return Content(script, "text/html");
            //    }
            //    else if (userData.Count() > 1)
            //    {
            //        string script = string.Format("<script>alert('账户信息出错,有重复信息出现！');location.href='{0}'</script>", Url.Action("Login", "Account", "User"));
            //        return Content(script, "text/html");
            //    }
            //    else
            //    {
            //        MigrateShoppingCart(info.username);
            //        FormsAuthentication.SetAuthCookie(info.username, true);
            //        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            //        {
            //            return Redirect(returnUrl);
            //        }
            //        else
            //        {
            //            return RedirectToAction("LoginIn", "Account");
            //        }

            //    }

            //}
            //else
            //{
            //    ModelState.AddModelError("", "用户名或密码不正确");
            //}
            //return View();
            #endregion
            string username = HttpUtility.HtmlEncode(info.username);
            string pwd = HttpUtility.HtmlEncode(info.password);

            var userData = logData.getLoginData(username, pwd);
            if (userData.Count() <= 0)
            {
                string script = string.Format("<script>alert('用户名和密码不一致！');location.href='{0}'</script>", Url.Action("Login", "Account", "User"));
                return Content(script, "text/html");
            }
            else if (userData.Count() > 1)
            {
                string script = string.Format("<script>alert('账户信息出错,有重复信息出现！');location.href='{0}'</script>", Url.Action("Login", "Account", "User"));
                return Content(script, "text/html");
            }
            else
            {
                //添加Cookie
                //HttpCookie cookie = new HttpCookie("myCookie");
                //cookie.Values.Add("name", username);
                //cookie.Values.Add("pwd", pwd);
                //cookie.Values.Add("UserId", userData.First().Id.ToString());
                //Response.AppendCookie(cookie);
                Session["UserId"] = userData.First().Id.ToString();
                Session["UserName"] = username;
                Session["PassWord"] = pwd;

                ViewBag.Name = username;

            }
            //当用户登录时将该用户名与购物车的信息关联起来

            //MigrateShoppingCart(username);
            MigrateShoppingCart(userData.First().Id.ToString());

            return RedirectToAction("Index", "Home");
        }

        //public ActionResult LoginIn()
        //{
        //    //查看Cookie
        //    //HttpCookie cookie = Request.Cookies["myCookie"];
        //    //if (cookie != null)
        //    //{
        //    //    ViewBag.Name = cookie["name"];
        //    //    return View();
        //    //}
        //    if (Session["UserName"] != null)
        //    {
        //        ViewBag.Name = Session["UserName"];
        //        var product = NewProduct.GetTopSellingProduct(3, 0);
        //        return View(product);
        //    }
        //    else
        //    {
        //        string script = string.Format("<script>alert('用户未登录');location.href='{0}'</script>", Url.Action("login", "Account", "User"));
        //        return Content(script, "text/html");
        //    }
        //}


        //退出系统
        public ActionResult LogOut()
        {
            //退出登陆之后清理Cookie
            //HttpCookie cookie = new HttpCookie("myCookie");
            //cookie.Expires = DateTime.Now.AddDays(-1);
            //Response.Cookies.Add(cookie);
            Session.Clear();
            return RedirectToAction("Index", "home");
        }

        //用户注册
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(Register UserModel)
        {
            if (string.IsNullOrEmpty(UserModel.UName))
            {
                ModelState.AddModelError("UserName", "用户名不能为空");
            }

            if (string.IsNullOrEmpty(UserModel.Password))
            {
                ModelState.AddModelError("PassWord", "密码不能为空");
            }
            else if (UserModel.Password.Length < 6)
            {
                ModelState.AddModelError("PassWord", "密码长度不能少于6位");
            }

            if (string.IsNullOrEmpty(UserModel.Phone))
            {
                ModelState.AddModelError("Phone", "手机号码不能为空");
            }
            else if (UserModel.Phone.Length < 11)
            {
                ModelState.AddModelError("Phone", "手机号码格式不正确");
            }

            if (ModelState.IsValid)
            {
                UserRegist user = new UserRegist();
                bool res = user.RegistData(UserModel);
                if (res == true)
                {
                    string script = string.Format("<script>alert('注册成功！');location.href='{0}'</script>", Url.Action("Login", "Account", "User"));
                    return Content(script, "text/html");
                }
                else
                {
                    string script = string.Format("<script>alert('注册失败！');location.href='{0}'</script>", Url.Action("Register", "Account", "User"));
                    return Content(script, "text/html");
                }
            }
            return View("Register");

        }

        private void MigrateShoppingCart(string UserId)
        {
            var cart = ShopCart.GetCart(this.HttpContext);
            //将购物车的GUID替换为用户的ID
            cart.MigrateCart(UserId);
            Session[ShopCart.CartSessionKey] = UserId;
            //Session["CartId"] = UserName;
        }


        public string UserInfo()
        {
            //返回JSon
            string UserInformation = "";
            if (Session["UserName"] != null)
            {
                UserInformation = logData.GetUserInfo(Session["UserName"].ToString()).ToString();
            }
            return UserInformation;

        }

    }
}